/* RMI-C Server: Echo Sample (V1) */

#include "rmi.h"
#include "echo.h"

/* ************************ SERVANT ************************ */

struct echo_t {
  int cnt;
};

struct echo_t echo1;

char * message = "hello world";

void echo_hello_(void* this, void* in, void* out)
{
  struct echo_t * echo = (struct echo_t *)this;
  char ** msg = (char**)out;
  *msg = message;
  printf("echo->hello (call %d)\n", echo->cnt);
  echo->cnt++;  
}

void echo_sum_(void* this, void* in, void* out)
{
  struct echo_t * echo = (struct echo_t *)this;
  printf("echo->sum (call %d)\n", echo->cnt);
  struct two_int_t * xy = (struct two_int_t *)in;
  int * z = (int*)out;
  *z = xy->x + xy->y;
  echo->cnt++;  
}

void echo_goodbye_(void* this, void* in, void* out)
{
  struct echo_t * echo = (struct echo_t *)this;
  printf("echo->goodbye (call %d)\n", echo->cnt);
  echo->cnt++;  
}

/* ************************ SERVER ************************ */

int main(int argc, char* argv[])
{
  int port = atoi(argv[1]);
  
  /* create server */
  SERVER * server = rmi_server_create(port);
  
  /* register methods */
  // rmi_server_register_interface_echo(server);
  rmi_server_register_method(server, __echo__, __echo_hello__,
    (xdrproc_t)xdr_void, (xdrproc_t)xdr_wrapstring, echo_hello_);
  rmi_server_register_method(server, __echo__, __echo_goodbye__,
    (xdrproc_t)xdr_void, (xdrproc_t)xdr_void, echo_goodbye_);
  rmi_server_register_method(server, __echo__, __echo_sum__,
    (xdrproc_t)xdr_two_int, (xdrproc_t)xdr_int, echo_sum_);

  /* activate objects */
   rmi_server_activate_object(server, __echo__, 1, &echo1);

  /* start server */
  rmi_server_run(server);
  
}
